package hanxx.menu.controller;

import hanxx.common.utils.AjaxResult;
import hanxx.common.utils.DateUtils;
import hanxx.common.utils.SnowflakeIdWorker;
import hanxx.menu.domain.SysMenu;
import hanxx.menu.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * edit:韩鑫鑫
 * 部门:产品研发三部
 **/
@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping ("/getDataList")
    public AjaxResult getDataList(){
        AjaxResult ajaxResult = new AjaxResult();
        List<SysMenu> menus = menuService.getDataList();
        ajaxResult.put("menusData",menus);
        return ajaxResult;
    }

    @PostMapping("/addMenuTree")
    public AjaxResult addMenuTree(@RequestBody SysMenu menu){
        menu.setMenuId(SnowflakeIdWorker.idWorker.nextId());
        menu.setCreateTime(DateUtils.getNowDate());

        menuService.save(menu);
        return AjaxResult.success("addMenuTree");
    }

    @PostMapping("/updateMenuTree")
    public AjaxResult updateMenuTree(@RequestBody SysMenu menu){
        SysMenu menu1 = menuService.get(menu.getMenuId());
        menuService.update(menu);
        return AjaxResult.success("updateMenuTree");
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteMenuTree(@PathVariable Long id){
        menuService.delete(id);
        return AjaxResult.success("deleteMenuTree");
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId)
    {
        List<SysMenu> menus = menuService.getDataList();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", menus);
        return ajax;
    }
}
